package com.pulse.base.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.pulse.base.domain.Device;
import com.pulse.base.mapper.DeviceMapper;
import com.pulse.base.service.*;
import com.pulse.common.annotation.DataScope;
import com.pulse.common.exception.ServiceException;
import com.pulse.common.utils.DateUtils;
import com.pulse.mqtt.MqttConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DeviceServiceImpl implements IDeviceService {

    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private IJobDispatchService jobDispatchService;
    @Autowired
    private IUserTestParamService userTestParamService;
    @Autowired
    private ICircuitThresholdService circuitThresholdService;
    @Autowired
    private ICircuitMeasureService circuitMeasureService;
    @Autowired
    private IAlarmService alarmService;

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<Device> selectDeviceList(Device device) {
        return deviceMapper.selectDeviceList(device);
    }

    /**
     * 根据设备id查询
     *
     * @param deviceId
     * @return
     */
    @Override
    public Device selectDeviceByDeviceId(Long deviceId) {
        return deviceMapper.selectDeviceByDeviceId(deviceId);
    }

    /**
     * 新增设备
     *
     * @param device
     * @return
     */
    @Override
    public int insertDevice(Device device) {
        Device check = deviceMapper.selectDeviceByDeviceNo(device.getDeviceNo());
        if(check != null) {
            throw new ServiceException("设备编号已存在");
        }
        device.setChannelNum(0L);
        device.setCreateTime(DateUtils.getNowDate());
        int res = deviceMapper.insertDevice(device);

        // 发送获取设备信息
        JSONObject deviceInfo = new JSONObject();
        deviceInfo.put("cmd","get_dev_info");
        deviceInfo.put("from","server");
        deviceInfo.put("moduleId","");
        deviceInfo.put("to",device.getDeviceNo());
        deviceInfo.put("uniId",device.getDeviceId());

        String msg = deviceInfo.toJSONString();
        MqttConsumer.publish("$FAMS/OTU/"+device.getDeviceNo()+"/ctrl-down",msg);
        return res;
    }

    /**
     * 修改设备
     *
     * @param device
     * @return
     */
    @Override
    public int updateDevice(Device device) {
        return deviceMapper.updateDevice(device);
    }

    /**
     * 删除设备
     *
     * @param deviceIds
     * @return
     */
    @Override
    @Transactional
    public int deleteDeviceByDeviceIds(Long[] deviceIds) {

        for (Long deviceId : deviceIds) {
            Device device = deviceMapper.selectDeviceByDeviceId(deviceId);
            // 删除jobDispatch任务
            jobDispatchService.deleteJobDispatchByDeviceNo(device.getDeviceNo());
            // 删除userTestParam
            userTestParamService.deleteByDeviceNo(device.getDeviceNo());
            // 删除circuitThreshold
            circuitThresholdService.deleteByDeviceNo(device.getDeviceNo());
            // 删除circuitMeasure(event refer file)
            circuitMeasureService.deleteCircuitMeasureByDeviceNo(device.getDeviceNo());
            // 删除alarm
            alarmService.deleteByDeviveNo(device.getDeviceNo());
        }

        return deviceMapper.deleteDeviceByDeviceIds(deviceIds);
    }

    /**
     * 根据设备编号查询
     *
     * @param deviceNo
     * @return
     */
    @Override
    public Device selectDeviceByDeviceNo(String deviceNo) {
        return deviceMapper.selectDeviceByDeviceNo(deviceNo);
    }

    /**
     * 更新设备连接状态
     *
     * @param deviceId
     * @param status
     * @return
     */
    @Override
    public int updateDeviceStatus(Long deviceId, String status) {
        return deviceMapper.updateDeviceStatus(deviceId, status);
    }

    /**
     * 获取全部设备列表
     *
     * @return
     */
    @Override
    public List<Device> selectAllDeviceList() {
        return deviceMapper.selectDeviceList(new Device());
    }

}
